## Lucid Article Only Study 
## Levendusky/Stecula Cambridge Element 
## Original version: Fall 2019
## This version: Winter 2021 

## Load necessary libraries 
library(tidyverse) 
library(stringr)
library(lubridate)
library(lmtest)
library(plm)
library(car) 
library(stargazer)

## Set the working directory & Read in the data 
#setwd("YOUR WORKING DIRECTORY HERE")
article <- read_csv("lucid_data_for_analysis.csv") 

## Code up key varaibles  
article <- article %>% 
  ## treatment code: 1 is control, 2 is polarization, 3 is consensus 
  mutate(treatment = ifelse(!is.na(q35_page_submit),1,
                            ifelse(!is.na(q36_page_submit),2,
                                   ifelse(!is.na(q37_page_submit),3,NA))),
  ## DVs
         other_ft = ifelse(q37 < 4,q7_2,
                           ifelse(q37 > 4,q7_1,NA)),
         other_cand = ifelse(q37 < 4,q7_4,
                             ifelse(q37 > 4,q7_3,NA)),
         same_ft = ifelse(q37 < 4,q7_1,
                          ifelse(q37 > 4,q7_2,NA)), 
         ft_diff = same_ft - other_ft, 
         pid_import = ifelse(q37 < 4,q4,
                             ifelse(q37 == 4,q6,
                                    ifelse(q37 > 4,q5,NA))),
         other_trust = ifelse(q37 < 4,q27_1,
                             ifelse(q37 > 4,q26_1,NA)),
         same_trust = ifelse(q37 < 4,q26_1,
                           ifelse(q37 > 4,q27_1,NA)), 
         ## trait items (scale is hi --> low, so reverse positive items)
         other_american = ifelse(q37 > 3, (-1*q28_1)+6,
                                 ifelse(q37 < 4, (-1*q29_1)+6,NA)),
         other_intelligent = ifelse(q37 > 3, (-1*q28_2)+6,
                                    ifelse(q37 < 4, q29_2,NA)),
         other_honest = ifelse(q37 > 3, (-1*q28_3)+6,
                               ifelse(q37 < 4, (-1*q29_3)+6,NA)),
         other_open = ifelse(q37 > 3, (-1*q28_4)+6,
                             ifelse(q37 < 4, (-1*q29_4)+6,NA)),
         other_generous = ifelse(q37 > 3, (-1*q28_5)+6,
                                 ifelse(q37 < 4, (-1*q29_5)+6,NA)),
         other_hypocritical = ifelse(q37 > 3, q28_6,
                                     ifelse(q37 < 4, q29_1,NA)),
         other_selfish = ifelse(q37 > 3, q28_7,
                                ifelse(q37 < 4, q29_7,NA)),
         other_mean = ifelse(q37 > 3, -1*q28_8,
                             ifelse(q37 < 4, -1*q29_8,NA)),
  ## Manipulation/Attention Checks 
         manip_check = ifelse((treatment == 1 & q34 == 2) | 
                                (treatment == 2 & q34 == 4) | 
                                (treatment == 3 & q34 == 5),1,0),
         attention = ifelse(q35 ==46,1,0))

## make data rowwise to calculate political knowledge, traits, common ground 
article <- article %>% 
  mutate(know1 = ifelse(q27 == 4,1,0),
         know2 = ifelse(q30 == 3,1,0),
         know3 = ifelse(q32 == 1,1,0)) %>% 
  rowwise(.) %>%
  mutate(pol_know = sum(c(know1,know2,know3)),
         avg_trait = mean(c(other_american, other_intelligent, other_honest,
                            other_open, other_generous, other_hypocritical,
                            other_selfish, other_mean), na.rm=T),
                  common_ground = mean(c(q16,q17)))

## re-scale DVs to [0,1] scale 
article <- article %>% 
  mutate(other_ft = other_ft/100,
         other_cand = other_cand/100,
         other_trust = other_trust/5, 
         avg_trait = avg_trait/5) 

## manipulation/attention checks
mean(article$manip_check, na.rm=T) 
mean(article$attention, na.rm=T) 

## treatment effects          
m1 <- lm(other_ft ~ as.factor(treatment),
           data = article)
m2 <- lm(same_ft ~ as.factor(treatment),
           data = article)
m3 <- lm(ft_diff ~ as.factor(treatment),
           data = article) 
m4 <- lm(other_cand ~ as.factor(treatment),
         data = article) 
m5 <- lm(other_trust ~ as.factor(treatment),
           data = article)
m6 <- lm(avg_trait ~ as.factor(treatment),
           data = article,
         subset = q37 != 4) 
m7 <- lm(common_ground ~ as.factor(treatment),
           data = article)
m8 <- lm(pid_import ~ as.factor(treatment),
           data = article)

## Output to a table 
stargazer(m1, m4, m5, m6, 
          column.labels = c("Out-Party FT",
                            "Out-Party Cand.","Out-Party Trust","Out-Party Traits"), 
          covariate.labels = c("Polarizing Article","Common Ground Article"),
          digits = 2, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/appendix_article_only_data.html")
